/**
 * Copyright (c) 华南农业大学信息学院蔡超敏2015版权所有
 * 
 * 文件创建时间：2015年4月7日
 */
package org.scauinfo.fpos.action.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.scauinfo.fpos.action.BaseAction;
import org.scauinfo.fpos.model.ProductModel;
import org.scauinfo.fpos.service.ProductService;
import org.scauinfo.fpos.service.ProductTypeService;
import org.scauinfo.fpos.util.ExceptionUtil;
import org.scauinfo.fpos.util.Json;
import org.scauinfo.fpos.vo.Product;
import org.scauinfo.fpos.vo.ProductType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author 蔡超敏
 * 
 */

@Namespace("/")
@Scope("prototype")
@Controller
@Action(value = "product", results = { @Result(name = "product", location = "/product.jsp") })
@RemoteProxy(name = "productAction")
public class ProductAction extends BaseAction implements ModelDriven<Product> {

	private static final long serialVersionUID = 3088843221530537631L;
	private static final Logger logger = Logger.getLogger(ProductAction.class);
	@Autowired
	private ProductService productService;

	@Autowired
	private ProductTypeService productTypeService;

	public ProductType productType = new ProductType();
	public Product product = new Product(productType);

	/**
	 * 得到数据列表
	 */
	public void grid() {
		System.out.println("tags ----1");
		int total = productService.getTotal();
		System.out.println("tags ----total" + total + "page" + page + "rows"
				+ rows + ", sort" + sort + ", order" + order);
		List<Product> list = productService.findAllProduct(page, rows, sort,
				order);
		System.out.println("tags ----2" + list.size());

		List<ProductModel> mList = new ArrayList<ProductModel>();

		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Product product = (Product) iterator.next();
			ProductModel productModel = new ProductModel(product.getId(),
					product.getProductId(), product.getProductName(),
					product.getProductPrice());
			productModel.setDescription(product.getDescription());
			productModel.setImage(product.getImage());

			if (product.getProductType() != null) {
				productModel.setProductTypeId(product.getProductType()
						.getProductTypeId());
				productModel
						.setTypeName(product.getProductType().getTypeName());
				productModel.setProductTypeDescription(product.getProductType()
						.getDescription());
			}
			mList.add(productModel);
		}
		writeJson(total, mList);
		System.out.println("tags ----3");
	}

	/**
	 * 添加操作
	 */
	public void add() {
		Json result = new Json();

		System.out.println("tags ----product" + product.getDescription());
		System.out.println("tags ----product" + product.getProductId());
		System.out.println("tags ----product"
				+ product.getProductType().getId());
		System.out.println("tags ----product"
				+ product.getProductType().getProductTypeId());
		try {
			product.setProductType(productTypeService.get(product
					.getProductType().getId()));
			System.out.println("tags ----product"
					+ product.getProductType().getId());
			System.out.println("tags ----product"
					+ product.getProductType().getProductTypeId());
			productService.saveProduct(product);
			result.setSuccess(true);
			result.setMsg("添加产品成功");
		} catch (Exception e) {
			logger.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("添加产品失败");
		}
		writeJson(result);
	}

	/**
	 * 修改操作
	 */
	public void edit() {
		Json result = new Json();
		try {
			System.out.println("edit ----product" + product.getId());
			System.out.println("edit ----product"
					+ product.getProductType().getId());
			product.setProductType(productTypeService.get(product
					.getProductType().getId()));
			System.out.println("edit ----product"
					+ product.getProductType().getId());
			System.out.println("tags ----product"
					+ product.getProductType().getProductTypeId());
			productService.updateProduct(product);
			result.setSuccess(true);
			result.setMsg("修改产品成功");
			result.setSuccess(true);
		} catch (Exception e) {
			logger.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("添加产品失败");
		}
		writeJson(result);
	}

	/**
	 * 删除操作
	 */
	public void remove() {
		Json result = new Json();
		try {
			System.out.println("tags ----=" + product.getId());
			productService.deleteProduct(product.getId());
			result.setSuccess(true);
			result.setMsg("删除产品成功");
		} catch (Exception e) {
			logger.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("删除产品失败");
		}
		writeJson(result);
	}

	@RemoteMethod
	public String getProducts(String keyword) {
		System.out.println("getProducts-----------");
		List<Product> list = productService.getProductsByKeyword(keyword);

		List<ProductModel> mList = new ArrayList<ProductModel>();

		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Product product = (Product) iterator.next();
			ProductModel productModel = new ProductModel(product.getId(),
					product.getProductId(), product.getProductName(),
					product.getProductPrice());
			productModel.setDescription(product.getDescription());
			productModel.setImage(product.getImage());

			if (product.getProductType() != null) {
				productModel.setProductTypeId(product.getProductType()
						.getProductTypeId());
				productModel
						.setTypeName(product.getProductType().getTypeName());
				productModel.setProductTypeDescription(product.getProductType()
						.getDescription());
			}
			mList.add(productModel);
		}
		System.out.println("getProducts-----------");
		Map<String, Object> maps = new HashMap<String, Object>();
		maps.put("product", mList);
		String json = JSON.toJSONString(maps);
		System.out.println("getProducts-----------" + json);
		return json;
	}
	
	@RemoteMethod
	public void addProductItem(Integer id) {
		Map<String, Object> session = ActionContext.getContext().getSession();
		List<Product> products = (List<Product>) session.get("salesorderitems");
		if (products == null) {
			products = new ArrayList<Product>();
		}
		System.out.println("products.size = " + products.size());
		Product aproduct=productService.get(id);
		for (Iterator iterator = products.iterator(); iterator.hasNext();) {
			Product product = (Product) iterator.next();
			if(aproduct.getId()==product.getId()){
				
			}
		}
		
		
		products.add(productService.get(id));
		session.put("salesorderitems", products);
	}

	@Override
	public Product getModel() {
		return product;
	}
}